package com.algorithm;


import java.util.ArrayList;
import java.util.List;

public class TreeNodePath {
    public static void main(String[] args) {
        TreeNodePath nodePath = new TreeNodePath();
        nodePath.binaryTreePaths(nodePath.initTreeNode());
    }

    private TreeNode initTreeNode() {
        TreeNode root = new TreeNode(1);
        TreeNode leftSecondNode = new TreeNode(2);
        TreeNode rightSecondNode = new TreeNode(3);
        root.left = leftSecondNode;
        root.right = rightSecondNode;

        TreeNode thirdLeftNode = new TreeNode(5);
        TreeNode thirdRightNode = new TreeNode(6);

        leftSecondNode.left = thirdLeftNode;
        leftSecondNode.right = thirdRightNode;
        return root;
    }

    public List<String> binaryTreePaths(TreeNode root) {
        List<String> path = new ArrayList<String>();
        if (root == null) {
            return path;
        }


        reverseTreeNode(root, "", path);
        return path;
    }

    private void reverseTreeNode(TreeNode node, String str, List<String> path) {
        if (node == null) {
            return;
        }
        str +=Integer.toString(node.val);

        TreeNode leftNode = node.left;
        TreeNode rightNode = node.right;
        if (leftNode == null && rightNode == null) {
            path.add(str);
            System.out.println(str);
            return;
        }
        str += "->" ;

        if (leftNode != null) {
            reverseTreeNode(leftNode, str, path);
        }

        if (rightNode != null) {
            reverseTreeNode(rightNode, str, path);
        }
    }
}
